import {Cache} from "../cache";

function DataCache(key, seconds, getData) {
    const localData = Cache.getJson(key);
    const now = parseInt(new Date().valueOf() / 1000);
    if (localData) {
        const pastSecond = now - localData.time;
        if (pastSecond < seconds) {
            //缓存有效期内，返回缓存数据
            return Promise.resolve(localData.data);
        }
    }
    return getData().then(data => {
        Cache.setJson(key, {
            time: now,
            data
        });
        return data;
    });
}

export {
    DataCache
}
